package com.bbs.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;

import com.bbs.bean.ReplyBBSOfBitAuto;
import com.car.util.ConnectionPool;
import com.car.util.Util;

public class ReplyDaoOfBitAuto {
	private Connection conn = ConnectionPool.getConnection("bitauto", "root", "zhouzhou100428", "localhost");
	private Connection co = ConnectionPool.getConnection("carvoice_core", "root", "zhouzhou100428", "localhost");
	private static Logger logger = Logger.getLogger(ReplyDaoOfBitAuto.class.getName());

	// 汽车urlSpell和ID的对应
	public Map<String, Integer> correspond() {
		Map<String, Integer> map = new HashedMap();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bitauto_bbs,bitauto_urlspell FROM core_brand_bitauto";
		try {
			st = co.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String key = rs.getString("bitauto_urlspell");
				int value = rs.getInt("bitauto_bbs");
				map.put(key, value);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

	// 插入数据库中
	public void insertReply(ReplyBBSOfBitAuto rep) {
		PreparedStatement ps = null;
		String sql = "INSERT INTO bitauto_yh_hf(hf_yhid,hf_clubid,hf_bbsid,hf_bt,hf_nr,hf_hfuid,hf_time) VALUES(?,?,?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, rep.getHf_yhid());
			ps.setInt(2, rep.getHf_clubid());
			ps.setInt(3, rep.getHf_bbsid());
			ps.setString(4, rep.getHf_bt());
			ps.setString(5, rep.getHf_nr());
			ps.setInt(6, rep.getHf_hfuid());
			ps.setDate(7, (Date) Util.getDate(rep.getHf_time()));
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	// 获取用户id
	public List<Integer> getUserID() {
		List<Integer> userIDs = new ArrayList<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT DISTINCT kb_yhid  FROM bitauto_kb WHERE kb_yhid>0";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int uid = rs.getInt("kb_yhid");
				userIDs.add(uid);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userIDs;
	}

}
